package cn.gdcp.sqlitetest;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class DatabasetProvider extends ContentProvider {
    static  UriMatcher uriMatcher;
  private   static final int  BOOKDIR=1;
    private   static final int  BOOKITE=2;
    SQLiteDatabase database;
      private   Cursor cursor;
    static final  String AUTHORITY="cn.gdcp.sqlitetest.provider";
    public DatabasetProvider() {
    }
   static {
    uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
       uriMatcher.addURI(AUTHORITY,"book",BOOKDIR);
       uriMatcher.addURI(AUTHORITY,"book/#",BOOKITE);
   }
    public int delete(Uri uri, String selection, String[] selectionArgs) {
             int deleteRow=0;
        switch (uriMatcher.match(uri)){
            case BOOKDIR:
                break;
            case BOOKITE:
                String bookId=uri.getPathSegments().get(1);
           deleteRow= database.delete("Book","id=?",new String[]{bookId});
                break;
            default:
                break;


        }
           return deleteRow;
    }

    @Override
    public String getType(Uri uri) {

        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
          Uri uriReturn=null;
           switch (uriMatcher.match(uri)){

               case BOOKDIR:
                   break;
                   case BOOKITE:
                       long id=database.insert("Book",null,values);
                       uriReturn=Uri.parse("content://"+AUTHORITY+"/book/"+id);
                       break;
               default:
                   break;


           }
                return uriReturn;
    }

    @Override
    public boolean onCreate() {
       database=new MyDatabaseHelper(getContext(),"BookStore",null,2).getReadableDatabase();
         return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
            cursor=null;
            switch(uriMatcher.match(uri)) {
                case BOOKDIR:
                cursor = database.query("Book", null, null, null, null, null, null);
                 break;
                case BOOKITE:
                    String bookId=uri.getPathSegments().get(1);
                    cursor=database.query("Book",null,"id=?",new String[]{bookId},null,null,null);
                    break;
                default:
                    break;
            }

        return  cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
             int updataRow=0;
            updataRow= database.update("Book",values,selection,selectionArgs);
            return updataRow;
    }
}
